import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import mapper.Mapper;
import model.Area;
import model.City;
import model.Province;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.net.URLDecoder;
import java.sql.*;
import java.util.Map;
import java.util.Set;

public class test {
    
    private SqlSessionFactory sqlSessionFactory;
    @Before
    public void setup() throws IOException {
        String resource="SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
    }
    
    
    @Test
    public void test1() throws IOException {
        String s1 = test.class.getClassLoader().getResource("").toString();
        s1= URLDecoder.decode(s1,"utf-8").substring(6);
        System.out.println(s1);
        File file=new File("");
        System.out.println(file.getAbsolutePath());
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("src/最完整的全国省市区数据.txt")));
        String s = "";
        StringBuffer sb = new StringBuffer();
        while ((s=br.readLine())!=null){
            sb.append(s);
        }
        System.out.println(sb.toString());
    }
    
    //省
    @Test
    public void test2() throws IOException {
        Gson gson=new Gson();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("src/最完整的全国省市区数据.txt")));
        String s = "";
        StringBuffer sb = new StringBuffer();
        while ((s=br.readLine())!=null){
            sb.append(s);
        }
        //System.out.println(sb.toString());
        JsonObject jsonObject=new JsonParser().parse(sb.toString()).getAsJsonObject();
        Set<Map.Entry<String, JsonElement>> set = jsonObject.entrySet();
        for (Map.Entry<String, JsonElement> entry : set) {
            Province province=new Province();
            province.setProvinceId(entry.getKey());
            //System.out.println(entry.getValue());
            JsonObject jsonObject1=new JsonParser().parse(String.valueOf(entry.getValue())).getAsJsonObject();
            province.setName(jsonObject1.get("name").getAsString());
            System.out.println(province);
            SqlSession session=sqlSessionFactory.openSession();
            Mapper mapper = session.getMapper(Mapper.class);
            mapper.insert_province(province);
            session.commit();
            session.close();
        }
        br.close();
    }
    
    //市
    @Test
    public void test3() throws IOException {
        Gson gson=new Gson();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("src/最完整的全国省市区数据.txt")));
        String s = "";
        StringBuffer sb = new StringBuffer();
        while ((s=br.readLine())!=null){
            sb.append(s);
        }
        //System.out.println(sb.toString());
        JsonObject jsonObject=new JsonParser().parse(sb.toString()).getAsJsonObject();
        Set<Map.Entry<String, JsonElement>> set = jsonObject.entrySet();
        for (Map.Entry<String, JsonElement> entry : set) {
            Province province=new Province();
            province.setProvinceId(entry.getKey());
            //System.out.println(entry.getValue());
            //entry.getKey():省的ID
            
            JsonObject jsonObject1=new JsonParser().parse(String.valueOf(entry.getValue())).getAsJsonObject();
            province.setName(jsonObject1.get("name").getAsString());
            //System.out.println(province);
            //System.out.println(entry.getValue());
    
            //System.out.println(jsonObject1.get("child"));
            JsonObject jsonObject2=new JsonParser().parse(String.valueOf(jsonObject1.get("child"))).getAsJsonObject();
            Set<Map.Entry<String, JsonElement>> set2 = jsonObject2.entrySet();
            for (Map.Entry<String, JsonElement> entry2 : set2) {
                String name = new JsonParser().parse(String.valueOf(entry2.getValue())).getAsJsonObject().get("name").getAsString();
                //entry2.getKey():市的ID
                //System.out.println(entry2.getKey()+" "+name);
                City city=new City();
                city.setProvinceId(entry.getKey());
                city.setCityId(entry2.getKey());
                city.setName(name);
    
                System.out.println(city);
                SqlSession session=sqlSessionFactory.openSession();
                Mapper mapper = session.getMapper(Mapper.class);
                mapper.insert_city(city);
                session.commit();
                session.close();
            }
        }
        br.close();
    }
    
    //区
    @Test
    public void test4() throws IOException {
        long start=System.currentTimeMillis();
        Gson gson=new Gson();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("src/最完整的全国省市区数据.txt")));
        String s = "";
        StringBuffer sb = new StringBuffer();
        while ((s=br.readLine())!=null){
            sb.append(s);
        }
        //System.out.println(sb.toString());
        JsonObject jsonObject=new JsonParser().parse(sb.toString()).getAsJsonObject();
        Set<Map.Entry<String, JsonElement>> set = jsonObject.entrySet();
        for (Map.Entry<String, JsonElement> entry : set) {
            Province province=new Province();
            province.setProvinceId(entry.getKey());
            //System.out.println(entry.getValue());
            //entry.getKey():省的ID
            
            JsonObject jsonObject1=new JsonParser().parse(String.valueOf(entry.getValue())).getAsJsonObject();
            province.setName(jsonObject1.get("name").getAsString());
            //System.out.println(province);
            //System.out.println(entry.getValue());
            
            //System.out.println(jsonObject1.get("child"));
            JsonObject jsonObject2=new JsonParser().parse(String.valueOf(jsonObject1.get("child"))).getAsJsonObject();
            Set<Map.Entry<String, JsonElement>> set2 = jsonObject2.entrySet();
            for (Map.Entry<String, JsonElement> entry2 : set2) {
                String name = new JsonParser().parse(String.valueOf(entry2.getValue())).getAsJsonObject().get("name").getAsString();
                //entry2.getKey():市的ID
                //System.out.println(entry2.getValue());
                JsonObject jsonObject3=new JsonParser().parse(String.valueOf(entry2.getValue())).getAsJsonObject();
                JsonObject jsonObject4 = jsonObject3.get("child").getAsJsonObject();
                Set<Map.Entry<String, JsonElement>> set4 = jsonObject4.entrySet();
                
                for (Map.Entry<String, JsonElement> entry4 : set4) {
                    
                    //System.out.println(entry4.getKey()+" "+entry4.getValue().getAsString());
                    Area area=new Area();
                    area.setCityId(entry2.getKey());
                    area.setAreaId(entry4.getKey());
                    area.setName(entry4.getValue().getAsString());
                    System.out.println(area);
                    
                    SqlSession session=sqlSessionFactory.openSession();
                    Mapper mapper = session.getMapper(Mapper.class);
                    mapper.insert_area(area);
                    session.commit();
                    session.close();
                }
            }
        }
        br.close();
        long end=System.currentTimeMillis();
        System.out.println((end-start));
    }
}
